package com.zmn.oms.third.tmall.service.impl;

import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.tmall.TmallConsts;
import com.zmn.oms.third.tmall.service.TmallService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 天猫勤鸽渠道对接订单状态处理实现
 *
 * @author xiewenbing
 * @date  2021/07/14 11:30
 */
@Slf4j
@Service("tmallNotifyService")
public class TmallNotifyServiceImpl implements OrderChangeNotifyService {

    @Autowired
    private TmallService tmallService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!TmallConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", TmallConsts.CHANNEL_NAME);
            return;
        }

        // 参数判断
        if (Objects.isNull(orderWorkLogChange)) {
            log.info("【{}】<订单状态同步> 参数为null", TmallConsts.CHANNEL_NAME);
            return;
        }

        // 录入方式和渠道订单id判断
        if (!Objects.equals(orderWorkLogChange.getInputType(),OrderConsts.ORDER_INPUT_TYPE_API) ||
                StringUtil.isBlank(orderWorkLogChange.getOuterId())) {
            log.info("【{}】的录入方式不为API接入，不需要状态同步", TmallConsts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", TmallConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try {
            switch (logType) {

                // 派单
                case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
                    tmallService.syncOrderDistribute(orderWorkLogChange);
                    break;
                // 挂起
                case OrderLogConsts.ORDER_LOG_TYPE_ORDER_SUSPEND:
                    tmallService.syncOrderSuspend(orderWorkLogChange);
                    break;
                // 改约
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                case OrderLogConsts.ORDER_LOG_TYPE_MODIFY_DUTY_TIME:
                case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                    tmallService.syncOrderDutytime(orderWorkLogChange);
                    break;
                // 签到
                case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
                    /*
                      天猫侧新增签到逻辑，用于统计上门及时率等信息
                      2022.06.07 by lhl
                     */
                    tmallService.syncOrderSignin(orderWorkLogChange);
                    break;
                // 核销
                case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                    /*
                      天猫侧业务逻辑调整，用户下单成功后，不会再给用户发送核销码，但是接口仍然需要核销码校验，
                      为满足天猫侧对接逻辑，工单点击完成时，默认核销码填写：0000
                      2022.06.06 by lhl
                     */
                    tmallService.validateVerificationCode(orderWorkLogChange.getOrderId(), "0000");
                    break;
                default:
                    log.info("【{}】<订单状态-【{}】不同步> ", TmallConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
                    return;
            }
        } catch (OmsBaseException exception) {
            log.info("【{}】<订单状态-【{}】同步> 失败 【{}】 ", TmallConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType),
                    exception.getMessage());
        }

        log.info("【{}】<订单状态-【{}】同步> 完成 ", TmallConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
    }
}
